<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { useI18n } from '@/hooks/web/useI18n'
// import { ElTag } from 'element-plus'
import { Table } from '@/components/Table'
import { getTableListApi, delTableListApi, doApsMoServerApi } from '@/api/apsMo'
import { useTable } from '@/hooks/web/useTable'
import { reactive, ref, unref } from 'vue'
import { useRouter } from 'vue-router'
import { useEventBus } from '@/hooks/event/useEventBus'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { BaseButton } from '@/components/Button'
import { ElMessage, ElMessageBox } from 'element-plus'
import { TableData } from '@/api/apsMo/types'

defineOptions({
  name: 'ParameterPage'
})

const { push } = useRouter()

const ids = ref<string[]>([])

const searchParams = ref({})
const setSearchParams = (params: any) => {
  searchParams.value = params
  getList()
}

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState
    const res = await getTableListApi({
      current: unref(currentPage),
      size: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list: res.data.records,
      total: res.data.total
    }
  },
  fetchDelApi: async () => {
    const res = await delTableListApi(unref(ids))
    return !!res
  }
})
const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList } = tableMethods

getList()

useEventBus({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      currentPage.value = 1
    }
    getList()
  }
})

const { t } = useI18n()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'selection',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    table: {
      type: 'selection'
    }
  },
  {
    field: 'index',
    label: t('tableDemo.index'),
    type: 'index',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    }
  },
  {
    field: 'scheduleDate',
    label: t('apsMo.scheduleDate'),
    search: {
      hidden: true
    }
  },
  {
    field: 'classTypeGroup',
    label: t('apsMo.classTypeGroup'),
    search: {
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '铸造甲班',
            value: '2'
          },
          {
            label: '铸造乙班',
            value: '1'
          }
        ],
        on: {
          change: (value: string) => {
            console.log(value)
          }
        }
      }
    }
  },
  {
    field: 'classTypeStr',
    label: t('apsMo.classType'),
    search: {
      hidden: true
    }
  },
  {
    field: 'scheduleCnt',
    label: t('apsMo.productCount'),
    search: {
      hidden: true
    }
  },
  {
    field: 'weightAll',
    label: t('apsMo.weightAll'),
    search: {
      hidden: true
    }
  },
  {
    field: 'weightOutAll',
    label: t('apsMo.weightOutAll'),
    search: {
      hidden: true
    }
  },
  {
    field: 'weightInAll',
    label: t('apsMo.weightIn'),
    search: {
      hidden: true
    }
  },
  {
    field: 'castWeight',
    label: t('apsMo.castWeight'),
    search: {
      hidden: true
    }
  },
  {
    field: 'action',
    width: '180px',
    label: t('tableDemo.action'),
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          return (
            <>
              <BaseButton type="success" onClick={() => action(data.row, 'detail')}>
                {t('exampleDemo.detail')}
              </BaseButton>
            </>
          )
        }
      }
    }
  },
  {
    field: 'scheduleStartDate',
    label: t('apsMo.scheduleStratDate'),
    hidden: true,
    search: {
      component: 'DatePicker',
      componentProps: {
        type: 'date',
        valueFormat: 'YYYY-MM-DD'
      }
    }
  },
  {
    field: 'scheduleEndDate',
    label: t('apsMo.scheduleEndDate'),
    hidden: true,
    search: {
      component: 'DatePicker',
      componentProps: {
        type: 'date',
        valueFormat: 'YYYY-MM-DD'
      }
    }
  }
])
//详情
const action = (row: TableData, type: string) => {
  push(`/apsBasics/aps-${type}?scheduleDate=${row.scheduleDate}&classType=${row.classType}`)
}
// @ts-ignore
const { allSchemas } = useCrudSchemas(crudSchemas)
//排程逻辑
const doApsMoServer = () => {
  ElMessageBox.confirm('是否执行轧辊计划预排程?', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    doApsMoServerApi()
    ElMessage({
      type: 'success',
      message: '异步执行成功，请等待'
    })
  })
}
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />

    <div class="mb-10px">
      <BaseButton type="primary" @click="doApsMoServer">{{ t('apsMo.apsMoServer') }}</BaseButton>
      <!-- <BaseButton :loading="delLoading" type="danger" @click="delData(null)">
        {{ t('exampleDemo.del') }}
      </BaseButton> -->
    </div>

    <Table
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
    />
  </ContentWrap>
</template>
@/hooks/event/useEventBus
